package org.ruanjj.springconvert;

class Solution {
    public String convert(String s, int numRows){
        int length = s.length();
        if(length <= numRows || length == 1 || numRows  == 1){
            return s;
        }
        int t = (numRows-2)+numRows;
        int c = length/t * (numRows-1);
        int l = c;
        if(length%t - numRows > 0){
            l = c+1+length%t%numRows;
        }else if(length%t != 0){
            l = c+1;
        }
        char[][] ZLayer = new char[numRows][l];
        for(int i = 0, x = 0, y = 0; i < length; i++){
            ZLayer[x][y] = s.charAt(i);
            if(i / (numRows-1) % 2 == 0){
                x++;
            }else{
                x--;
                y++;
            }
        }

        StringBuilder res = new StringBuilder();
        for(int i = 0; i < ZLayer.length; i++){
            for(int j = 0; j < ZLayer[i].length; j++){
                if(ZLayer[i][j] != 0){
                    res.append(ZLayer[i][j]);
                }
            }
        }
        return res.toString();
    }
}
